package com.gmzg.gmzgnews.news.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gmzg.gmzgnews.R;
import com.gmzg.gmzgnews.news.entries.AmuseNews;
import com.gmzg.gmzgnews.news.entries.HeadNews;
import com.squareup.picasso.Picasso;

import java.util.Collection;
import java.util.List;

/**
 * Created by Jasen on 2016 2016/11/4.11:16
 * Email:1142803753@qq.com
 * weixin: aserbao
 */

public class NewsAmuseAdapter extends RecyclerView.Adapter implements
        View.OnClickListener {
    private static final int NEWS_AMUSE_MUTI = 4;
    private static final int NEWS_AMUSE_SIGNLE = 1;
    private Context mContext;
    private List<AmuseNews> mAmuseNewsList;
    private RecyclerView mRecyclerView;
    private int mRetPosition;

    public NewsAmuseAdapter(Context context, List<AmuseNews> amuseNewsList) {
        mContext = context;
        mAmuseNewsList = amuseNewsList;
    }

    /*@Override
    public int getItemViewType(int position) {
        mRetPosition = 1;
        *//*if (mAmuseNewsList != null) {
            if (mAmuseNewsList.get(position).getImg().equals("/top/default.jpg")){
                mRetPosition = NEWS_AMUSE_MUTI;
            }else{
                mRetPosition = NEWS_AMUSE_SIGNLE;
            }
        }*//*
        return mRetPosition;
    }*/

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.mRecyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.mRecyclerView = null;
    }

    public void addAll(Collection<? extends AmuseNews> collection){
        mAmuseNewsList.addAll(collection);
        notifyDataSetChanged();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder ret = null;
        View view = null;
        view = LayoutInflater.from(mContext)
                .inflate(R.layout.news_head_item, parent, false);
        ret = new MyViewHolder(view);
       /* switch (viewType) {
            case NEWS_AMUSE_MUTI:

                break;
            case NEWS_AMUSE_SIGNLE:
                view = LayoutInflater.from(mContext)
                        .inflate(R.layout.news_head_item, parent, false);
                ret = new MyViewHolder(view);
                break;
        }*/
//        view.setOnClickListener(this);
        return ret;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        MyViewHolder holder = null;
        AmuseNews news = mAmuseNewsList.get(position);
       if (h instanceof  MyViewHolder) {
          holder = (MyViewHolder) h;
       }
        if (holder != null) {
            Picasso.with(mContext)
                    .load("http://tnfs.tngou.net/img"+news.getImg())
                    .into(holder.image);
            holder.title.setText(news.getTitle());
            holder.source.setText(news.getFromname());
        }
        /*switch (mRetPosition) {
            case NEWS_AMUSE_SIGNLE:

                break;
            case NEWS_AMUSE_MUTI:
                break;
        }*/

    }

    @Override
    public int getItemCount() {
        int ret = 0;
        if (mAmuseNewsList != null) {
            ret = mAmuseNewsList.size();
        }
        return ret;
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(mContext, "被点击了", Toast.LENGTH_SHORT).show();
    }

    private static class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView title;
        private TextView source;
        private ImageView image;

        public MyViewHolder(View itemView) {
            super(itemView);
            title = ((TextView) itemView.findViewById(R.id.item_title));
            source = ((TextView) itemView.findViewById(R.id.item_type));
            image = ((ImageView) itemView.findViewById(R.id.item_image));
        }
    }
    public interface OnChildClickListener{
        void onChildClick(RecyclerView parent, View view, int position, HeadNews data);
    }
}
